<!--
  TODO
    tip
    event
    行宽计算
-->

<template>
  <div class="fieldset-wrapper" v-show="fieldsetShow" :key="fieldset.code">
      <div v-if="fieldset.name" class="fieldset-header" @click="collaspeClick">
        <h4 v-html="fieldset.name"></h4>
        <div class="collapse-box">
          <Icon v-if="collapse" type="chevron-down"></Icon>
          <Icon v-else type="chevron-up"></Icon>
        </div>
      </div>

      <Row class="fieldset-body" :class="{'fieldset-body-collapse':collapse}">
        <Col :span="fieldGroupCol" v-for="(field,index) in fieldset.fields" class="field-group" v-show="fields[field.code].show!==false" :class="groupClass(field)" :key="field.code">
          <template v-if="!fullRow(field)">
            <Col class="field-label" :span="fieldLabelCol">
              <label :class="{reuqire:field.required&&field.editable!==false}">
                {{field.name}}
              </label>
            </Col>
            <Col class="field-input" :class="inputClass()" :span="fieldControlCol">
              <component class="form-control" :code="field.code" :is="fieldControl(field)"></component>
            </Col>
          </template>
          <template v-else>
            <Col class="field-label" :span="fieldLabelCol/col">
              <label :class="{reuqire:field.required&&field.editable!==false}">
                {{field.name}}
              </label>
            </Col>
            <Col class="field-input" :class="inputClass()" :span="24-fieldLabelCol/col">
              <component class="form-control" :code="field.code" :is="fieldControl(field)"></component>
            </Col>
          </template>
        </Col>
        <div class="clear"></div>
      </Row>

    </div>
</template>

<script>
import Fields from "field/rebuild";
import FieldsType from "field/type";
export default {
  subModuleStore: true,
  props: {
    fieldset: {},
    col: {
      default: 2
    }
  },
  data() {
    return {
      collapse: this.fieldset.collapse
    };
  },
  computed: {
    fieldsetShow() {
      if (!this.fieldset.fields.length) {
        return this.fieldset.fields.length;
      } else {
        for (let field of this.fieldset.fields) {
          if (this.fields[field.code].show !== false) {
            return true;
          }
        }
        return false;
      }
    }, //一个输入字段组的占行
    fieldGroupCol() {
      return 24 / this.col;
    },
    //字段名占行
    fieldLabelCol() {
      return 4;
    },
    //字段控件占行
    fieldControlCol() {
      return 24 - this.fieldLabelCol;
    }
  },
  methods: {
    collaspeClick() {
      this.collapse = !this.collapse;
    },
    inputClass() {
      return {};
    },
    groupClass(field) {
      return {
        "group-full-row": this.fullRow(field)
      };
    },
    fieldControl(field) {
      return Fields.fieldType(field);
    },
    fullRow(field) {
      return FieldsType[field.type] ? !!FieldsType[field.type].fullRow : false;
    }
  },
  components: {
    ...Fields.control
  }
};
</script>

<style lang="less">
.form .fieldset-wrapper {
  margin-bottom: 20px;
  border-bottom: 1px solid #e9eaec;
  padding-bottom: 8px;
  .fieldset-header {
    position: relative;
    border-left: 4px solid #168cf3;
    font-size: 12px;
    padding: 4px 12px;
    margin-bottom: 8px;
    cursor: pointer;
    &:hover {
      border-color: #57a3f3;
    }
    .collapse-box {
      position: absolute;
      right: 4px;
      top: 4px;
      float: right;
      font-size: 14px;
    }
  }
  .field-has-tip {
    .field-control {
      width: 60%;
      float: left;
    }
    .field-tip {
      width: 40%;
      float: left;
      line-height: 31px;
      padding: 0 16px;
    }
  }
  .fieldset-body {
    position: relative;
    transition: height 0.35s ease;
    &.fieldset-body-collapse {
      height: 0;
      overflow: hidden;
    }
  }
}
.field-group-tabs {
  margin: 16px;
  .ivu-tabs {
    /*padding-top: 32px;*/
  }
  .ivu-tabs-nav .ivu-tabs-tab {
    font-size: 13px;
  }
}
.form .field-group.group-full-row {
  width: 100%;
}
</style>
